package 链表;

public class ListNode {
    int data;
    ListNode next;

    ListNode(int data){
        this.data = data;
    }

    void addData(int newVal){
        ListNode newNode = new ListNode(newVal);
        if(this.next == null){
            this.next = newNode;
        }else{
            this.next.addData(newVal);
        }
    }

    public void print(){
        System.out.println(this.data);
        /*while(this.next != null){
            this.next.print();
        }*/
        if(this.next != null){
            this.next.print();
        }
    }
}

class Main{
    public static void main(String[] args) {
        ListNode newNode = new ListNode(1);
        newNode.addData(2);
        newNode.addData(3);
        newNode.print();
    }
}